﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DatabaseCore;
using Eaktest;
using SystemCore;

namespace EakTest
{
    public partial class OrderInquiry : BaseForm
    {
        private IOrderDao orderDao = new OrderDao();

        private IList<tbl_Order> OrderDataSource { get; set; }


        public OrderInquiry()
        {
            InitializeComponent();

            GetOrder();
            BindData();
            BindComboBox();
        }

        #region Private method

        private void BindComboBox()
        {
            cboOrderStatus.DataSource = Enum.GetValues(typeof (EOrderStatus));
        }

        private void GetOrder()
        {
            OrderDataSource = orderDao.Search(null);
            if (IsNotFound())
                return;
        }

        private void ClearGridView()
        {
            gvOrder.Rows.Clear();
            gvOrder.DataSource = null;
        }

        private void BindData()
        {
            ClearGridView();

            int i = 0;
            foreach (var item in OrderDataSource)
            {
                gvOrder.Rows.Add();
                gvOrder[(int) EOrderCol.OrderNo, i].Value = item.OrderNo;
                gvOrder[(int) EOrderCol.Status, i].Value = Enum.GetName(typeof (EOrderStatus), item.Status);

                i++;
            }
        }

        private bool IsNotFound()
        {
            if (OrderDataSource == null || OrderDataSource.Count == 0)
            {
                ClearGridView();
                MessageDialog.Information("Search not found");
                return true;
            }

            return false;
        }

        #endregion

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string orderNo = txtOrderNo.Text.Trim();
            //int orderStatus = Convert.ToInt32(cboOrderStatus.SelectedValue);
            int orderStatus = OrderUtility.GetOrderStatusByRole(UserContext.Role);

            OrderDataSource = orderDao.Search(orderNo, orderStatus);
            if (IsNotFound())
                return;

            BindData();
        }

        private void txtOrderNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char) Keys.Return)
            {
                btnSearch.PerformClick();
            }
        }

        private void gvOrder_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 &&
                ((DataGridView) sender).Columns[e.ColumnIndex].GetType() == typeof (DataGridViewLinkColumn))
            {
                string orderNo = gvOrder[e.ColumnIndex, e.RowIndex].Value.ToString();
                //MessageBox.Show(orderNo);
                new OrderDetailInquiry(orderNo).ShowDialog();
            }
        }
    }
}